/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.cast.teseg.Listeners;

import edu.cast.teseg.controladores.Controlador;
import edu.cast.teseg.controladores.InterfaceControlador;
import edu.cast.teseg.gui.GuiPrincipal;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ItemListener;
/**
 *
 * @author edgarin
 */
public class ManejoEventos implements MouseListener,ChangeListener, ActionListener, ItemListener, InterfaceControlador{
    private GuiPrincipal guiGL;
    private Controlador controlGL;

    public ManejoEventos(){}

    @Override
    public void setControlador(Controlador x){ this.controlGL = x; }

    @Override
    public void setGuiPrincipal(GuiPrincipal x){ this.guiGL = x; }

    //*** metodo para controlar los eventos de botones GUI ***//
    @Override
    public void mouseClicked(MouseEvent e) {
        boolean haga;
        boolean haga2;
       
        if(e.getSource() == this.guiGL.getGenerarBtn()){
        haga = this.controlGL.isNumeric(this.guiGL.getNumNodos().getText());
        haga2 = this.controlGL.isNumeric(this.guiGL.getContProb().getText().replaceAll("\\.", ""));
        
        if(!haga){
                this.controlGL.setMensajeErrores("Vertice no numerico o no entero");                       
        }
         else if(!haga2){
                this.controlGL.setMensajeErrores("error en el archivo un dato no es numero");
            }
         else if(haga || haga2){
                this.controlGL.iniciarGraficoGrafo(Integer.parseInt(this.guiGL.getNumNodos().getText()),Double.parseDouble(this.guiGL.getContProb().getText()));              
         }   
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    //*** metodo para agregar listener la barra deslizadora ***//
    @Override
    public void stateChanged(ChangeEvent e) {
        if(e.getSource() == this.guiGL.getBarraProb()){
            this.guiGL.getContProb().setText(Double.toString(this.guiGL.getBarraProb().getValue()/100.));            
            this.controlGL.iniciarGraficoGrafo(Integer.parseInt(this.guiGL.getNumNodos().getText()),Double.parseDouble(this.guiGL.getContProb().getText()));
        }        
    }

    //*** metodo para controlar eventos del menu ***//s
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()== this.guiGL.getItemSalir()){
            System.exit(0);
        }
        
        else if(e.getSource()== this.guiGL.getItemAbrir()){
            this.controlGL.subirArchivos();
        }
        
        else if(e.getSource()== this.guiGL.getItemGuardar()){
             this.controlGL.salvarArchivoGrafo();
        }
        
        else if(e.getSource()== this.guiGL.getItemContenido()){
            this.guiGL.mensajeAyuda();
        }
        
        else if(e.getSource()== this.guiGL.getItemAcercaDe()){
            this.guiGL.mensajeDeAcercaDe();
        }
        
        else if(e.getSource()== this.guiGL.getItemLicencia()){
            this.guiGL.mensajeLicencia();
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
      if(e.getSource()==this.guiGL.getIngenuo()){
          this.controlGL.setTipoAlgoritmo("ingenuo");
      }

      else if(e.getSource()==this.guiGL.getIngenuoOpt()){
          this.controlGL.setTipoAlgoritmo("ingenuoOpt");
      }

      else if(e.getSource()==this.guiGL.getVoraz()){
          this.controlGL.setTipoAlgoritmo("voraz");
      }
      
      else if(e.getSource()==this.guiGL.getDinamico()){
          this.controlGL.setTipoAlgoritmo("dinamico");
      }
    }

}